package photoEffects
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.BitmapDataChannel;
	import flash.display.BlendMode;
	import flash.display.Shader;
	import flash.display.Shape;
	import flash.filters.ColorMatrixFilter;
	import flash.filters.ShaderFilter;
	import flash.geom.Point;
	import flash.utils.ByteArray;
	
	import org.casalib.events.ProcessEvent;
	
	public class CrossProcessingEffect extends PhotoEffect
	{
		
		/**
		 * Constructor.
		 */
		public function CrossProcessingEffect()
		{
			super();
		}
		
		public static const NAME:String = "Cross-Processing";
		
		/**
		 * Apply the effect on a Bitmap. When the apply is finished, a ProcessEvent.COMPLETE is dispatched.
		 * @param bitmap The source Bitmap.
		 * @param callBack A function that accept a Bitmap object. Called when apply is finished.
		 */
		public override function applyOn(bitmap:Bitmap, callBack:Function = null):void
		{
			_workingBitmap = new Bitmap(bitmap.bitmapData.clone());
			
			
			
			var rBitmap:BitmapData = _workingBitmap.bitmapData.clone();
			rBitmap.applyFilter(_workingBitmap.bitmapData,_workingBitmap.bitmapData.rect,new Point(),getContrastFilter(0.15));
			_workingBitmap.bitmapData.copyChannel(rBitmap,_workingBitmap.bitmapData.rect,new Point(),BitmapDataChannel.RED,BitmapDataChannel.RED);
			
			var gBitmap:BitmapData = _workingBitmap.bitmapData.clone();
			gBitmap.applyFilter(_workingBitmap.bitmapData,_workingBitmap.bitmapData.rect,new Point(),getContrastFilter(0.25));
			_workingBitmap.bitmapData.copyChannel(rBitmap,_workingBitmap.bitmapData.rect,new Point(),BitmapDataChannel.GREEN,BitmapDataChannel.GREEN);
			
			var bBitmap:BitmapData = new BitmapData(_workingBitmap.width,_workingBitmap.height,true,0x000000);
			bBitmap.copyChannel(_workingBitmap.bitmapData,bBitmap.rect,new Point(),BitmapDataChannel.BLUE,BitmapDataChannel.BLUE);
			
			
			var s:Shader = new Shader(new ShaderClass_levels() as ByteArray);
			s.data.red.value = [0.18,1];
			shaderFilter_levels = new ShaderFilter(s);
			_workingBitmap.bitmapData.applyFilter(_workingBitmap.bitmapData,_workingBitmap.bitmapData.rect,new Point(),shaderFilter_levels);
			
			_workingBitmap.bitmapData.draw(bBitmap,null,null,BlendMode.OVERLAY,null,true);
			
			var yellowShape:Shape = new Shape();
			yellowShape.graphics.beginFill(0xEEFF00,0.05);
			yellowShape.graphics.drawRect(0,0,_workingBitmap.width,_workingBitmap.height);
			yellowShape.graphics.endFill();
			yellowShape.cacheAsBitmap = true;
			_workingBitmap.bitmapData.draw(yellowShape,null,null,BlendMode.LAYER,null,true);
			
			_resultBitmap = _workingBitmap;
			_workingBitmap = null;
			
			dispatchEvent(new ProcessEvent(ProcessEvent.COMPLETE));
			
			if(callBack != null){
				callBack(_resultBitmap);
			}
		}
		
		[Embed(source="../../pb/levels.pbj", mimeType="application/octet-stream")]
		private static var ShaderClass_levels:Class;
		private var shaderFilter_levels:ShaderFilter;
		
		private function getContrastFilter(value:Number):ColorMatrixFilter{
			var a:Number = value * 11;   
			var b:Number = 63.5 - (value * 698.5); 
			return  new ColorMatrixFilter([	a, 0, 0, 0, 
											b, 0, a, 0, 
											0, b, 0, 0, 
											a, 0, b, 0, 
											0, 0, 1, 0]);
		}
	}
}